#include "cookingdata.h"
#include <QtGui/QGuiApplication>
CookingData::CookingData(QObject *parent):
    QObject(parent)
{
    databaseUp();
}


CookingData::~CookingData()
{
    db.close();
}

void CookingData::go(const QString &from, const QString &to, const QString &steps, const QString &step)
{
    int _c =from.toInt();
    int _cend =to.toInt();
    timeInterval = step.toInt();
    totalStep = steps.toInt();
    for(_c; _c<=_cend; _c++)
        inBusiness(QString::number(_c));
    foreach (QString _s, inBuList)
    {
        checkIs( _s );
        kReport(_s);
    }
    resParser();
}
int CookingData::databaseUp()
{
    db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("Driver={SQL SERVER};Server=192.168.0.199;Database=Adroit_DB;Trusted_Connection=yes;");
    if (!db.open()){

    }
    return 1;
}

int CookingData::inBusiness(const QString&kIndex)
{
    qApp->processEvents();
    QString _s;
    _s
            .append( "SELECT TOP 1 started FROM Adroit_K" )
            .append( kIndex );
    QSqlQuery _sq;
    if (_sq.exec(_s))
        if(_sq.first())
            inBuList.append(kIndex);
        else
            qDebug()<<_sq.lastError().text();
    else
        qDebug()<<_sq.lastError().text();
    return 1;
}

int CookingData::kReport(const QString &kIndex)
{
    qApp->processEvents();
    QList<bool> _lst_lv1;
    QList<QList<bool>> _lst_lv2;
    QString _s;
    QSqlQuery _sq;
    QString _d;
    int _c =1;
    while (_c<=totalStep)
    {
        _d = QDateTime::currentDateTime().addSecs(-(timeInterval*3600*_c)).toString("yyyy-MM-dd hh:mm:ss.zzz" );
        _s
                .append( " SELECT TOP 1" )
                .append( " ch1_chOff," )
                .append( " ch2_chOff," )
                .append( " ch3_chOff," )
                .append( " ch4_chOff," )
                .append( " ch5_chOff," )
                .append( " ch6_chOff," )
                .append( " ch7_chOff," )
                .append( " ch8_chOff" )
                .append( " FROM Adroit_K" )
                .append( kIndex )
                .append( " WHERE TimestampStr <" )
                .append( "  '" )
                .append( _d )
                .append( "' ORDER BY TimestampStr DESC" );
        if (_sq.exec(_s))
            if(_sq.first())
                _lst_lv1<<_sq.value(0).toBool()<<_sq.value(1).toBool()<<_sq.value(2).toBool()<<_sq.value(3).toBool()<<_sq.value(4).toBool()<<_sq.value(5).toBool()<<_sq.value(6).toBool()<<_sq.value(7).toBool();
            else
                qDebug()<<_sq.lastError().text();
        else
            qDebug()<<_sq.lastError().text();
        _s.clear();
        _lst_lv2.append(_lst_lv1);
        _lst_lv1.clear();
        _c++;
    }
    res.insert(kIndex,_lst_lv2);
    return 1;
}

int CookingData::resParser()
{
    qApp->processEvents();
    short int _c = 0;
    short int _index_01 = 1;//канал
    short int _index_02 = 0;//шаг
    bool _arr[8];
    QString _res;
    QMap<QString, QList<QList<bool>>>::const_iterator _iterator_lv1 = res.constBegin();
    while (_iterator_lv1 != res.constEnd())
    {
        _index_02=0;
        QList<QList<bool>>::const_iterator _iterator_lv2 = _iterator_lv1.value().constBegin();
        while (_iterator_lv2 != _iterator_lv1.value().constEnd())
        {
            _index_01=1;

            QListIterator<bool> _iterator_lv3(*_iterator_lv2);
            while (_iterator_lv3.hasNext())
            {
                if ((_arr[_c] != _iterator_lv3.next()) and (_index_02 > 0))
                {

                    _res="K"+_iterator_lv1.key()+"_"+QString::number(_index_01)+" ~"+QString::number(_index_02*timeInterval)+" hours left";
                    bingoIs( _res );
                }
                _arr[_c] = _iterator_lv3.peekPrevious();
                _c++;
                if(_c == 8)
                    _c = 0;
                _index_01++;
            }
            _index_02++;
            _iterator_lv2++;
        }
        _iterator_lv1++;
    }
    qDebug()<<"done";
    return 1;
}
